<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="登录账户设置" name="account">
            <el-row type="flex" justify="center" style="padding-right: 300px">
              <el-form :model="formData" :rules="rules" label-width="100px">
                <el-form-item prop="username" label="姓名">
                  <el-input v-model="formData.username" />
                </el-form-item>
                <el-form-item prop="newPassword" label="密码">
                  <el-input v-model="formData.newPassword" type="password" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updateAccount">更新</el-button>
                  <el-button @click="$router.back()">取消</el-button>
                </el-form-item>
              </el-form>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="个人详情" name="detail">
            <component :is="userInfoName" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="job">
            <component :is="jobInfoName" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from '@/views/employees/components/user-info'
import JobInfo from '@/views/employees/components/job-info'
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userInfoName: 'UserInfo',
      jobInfoName: 'JobInfo',
      activeName: 'account',
      userId: this.$route.params.id,
      formData: {
        username: '',
        newPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      const data = await getUserDetailById(this.userId)
      console.log(data)
      this.formData = data
    },
    async updateAccount() {
      await saveUserDetailById({ ...this.formData, password: this.formData.newPassword })
    }
  }
}
</script>

<style>

</style>
